<template>
  <div class="bg">
    <el-row>
      <el-col :sm="{ span: 15, offset: 5 }" :xs="{ span: 24, offset: 0 }">
        <div class="user-profile-header">
          <el-tooltip effect="light" content="点击查看大头像" placement="top">
          <el-avatar :src="infoObj.imageUrl"
                     class="avatar"
                     @click.native="openLargeAvatar(infoObj.imageUrl)"/>
          </el-tooltip>
          <div class="user-info">
            <h3 class="r" @click="r">{{ infoObj.userName }}</h3>
            <div class="details">
              <span>UID: {{ infoObj.uid }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    IP地址: {{ infoObj.ip }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    账户状态: {{ infoObj.status === 0 ? '锁定' : '正常' }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    用户类型: {{
                  infoObj.userType === 0 ? '普通用户' :
                    infoObj.userType === 1 ? '审核员' :
                      infoObj.userType === 2 ? '管理员' : '未知'
                }}</span>
            </div>
          </div>
        </div>
      <!-- 消息盒子 -->
      <el-row v-for="(item, key) in contentObj" :key="key">
        <div class="xxbox">
          <!-- 头像，用户名 -->
          <div class="ty">
            <el-row :gutter="24">
              <el-col :sm="{ span: 2 }" :xs="{ span: 5, offset: 0 }"
              >
                <el-avatar
                  class="ssss"
                  shape="square"
                  :size="50"
                  :src="item.param.uImage"
                ></el-avatar
                >
              </el-col>
              <el-col :span="7"
              >
                <div class="sub-title">{{ item.param.uName }}</div>
              </el-col
              >
              <el-col :span="11"
              ><span class="time">{{ item.createTime }}</span></el-col
              >
            </el-row>

            <el-row :gutter="24">
              <el-col :sm="{ span: 2 }" :xs="{ span: 5, offset: 0 }"
              >
                <div class="wwe"></div
                >
              </el-col>
              <el-col :sm="{ span: 22 }" :xs="{ span: 24, offset: 0 }">
                <!-- 内容 -->
                <p class="pre-wrap-text">{{ item.content }}</p>
                <!-- 图片 -->
                <div v-if="item.ctype === 1">
                  <el-row>
                    <div v-for="(sum, key) in item.pictureUrl" :key="key">
                      <el-col
                        :sm="{ span: 6, offset: 1 }"
                        :xs="{ span: 7, offset: 1 }"
                      >
                        <div>
                          <el-image
                            style="border-radius: 10px"
                            :style="imageStyle"
                            fit="true"
                            :src="sum"
                            :preview-src-list="item.pictureUrl"
                          >
                          </el-image>
                        </div>
                      </el-col>
                    </div>
                  </el-row>
                </div>
                <!-- 视频 -->
                <div v-if="item.ctype === 2">
                  <el-col
                    :sm="{ span: 20, offset: 1 }"
                    :xs="{ span: 23, offset: 1 }"
                  >
                    <video
                      style="border-radius: 10px"
                      align="center"
                      :autoplay="false"
                      width="100%"
                      :height="videoStyle"
                      aspectRatio="7:4"
                      fluid="true"
                      preload="auto"
                      muted=""
                      controls="true"
                    >
                      <source :src="item.pictureUrl[0]" type="video/mp4"/>
                      您的浏览器不支持视频标签！
                    </video>
                  </el-col>
                </div>
              </el-col>
            </el-row>

            <!-- 点赞,评论 -->
            <div class="dz">
              <el-row :gutter="24">
                <el-col :span="8">
                  <div
                    class="sz copy-btn"
                    data-clipboard-action="copy"
                    @click="copys($event, item.cid)"
                  >
                    <img
                      width="20"
                      height="20"
                      src="~assets/images/fenxiang.png"
                    />
                    分享链接
                  </div>
                </el-col
                >
                <div @click="dz(item.cid)">
                  <el-col :span="8">
                    <img
                      width="20"
                      height="20"
                      v-if="item.param.uIsZan === false"
                      src="~assets/images/dz.png"
                    />
                    <img
                      width="20"
                      height="20"
                      v-if="item.param.uIsZan === true"
                      src="~assets/images/dz1.png"
                    />
                    <span class="sz">点赞：{{ item.param.cNums }}</span>
                  </el-col>
                </div>

                <el-col :span="8">
                  <div @click="pin(item.cid)">
                    <img
                      width="20"
                      height="20"
                      src="~assets/images/pinlun.png"
                    />
                    <span class="sz">评论：{{ item.param.cTotal }}</span>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-row>
      <div class="fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="currentPage"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
      </el-col>
    </el-row>
    <!-- 大头像对话框 -->
    <el-dialog :visible.sync="isDialogVisible" style="width: 60%">
      <img :src="largeAvatarUrl" alt="大头像" style="width: 100%">
    </el-dialog>
  </div>
</template>

<script>
import touristApi from "@/api/tourist";
import Clipboard from "clipboard";
import {Message} from "element-ui";
import cookie from "js-cookie";
import operateApi from "@/api/Operate";

export default {
  data() {
    return {
      total: 0,
      contentObj: [],
      infoObj:[],
      currentPage: 1,
      page: 1,
      imageStyle: "",
      videoStyle: "",
      isDialogVisible: false, // 控制对话框的显示
      largeAvatarUrl: ''      // 存储大头像的URL
    }
  },
  mounted() {
    this.uid = this.$route.query.uid;
    this.getContent(this.uid);
    this.getInfo(this.uid);
    let width = document.documentElement.clientWidth;
    let height = document.documentElement.clientHeight;
    if (width < 720) {
      this.imageStyle = "width: 100%; height: 100px;";
      this.videoStyle = height / 4 + "px;";
    } else {
      this.imageStyle = "width: 100%; height: " + width / 6 + "px;";
      this.videoStyle = height / 3 + "px;";
    }
  },

  methods: {
    //复制分享链接
    copys(e, cid) {
      const hostname = window.location.origin;
      const clipboard = new Clipboard(e.target, {
        text: () => hostname + "/function/comments?cid=" + cid,
      });
      clipboard.on("success", () => {
        const h = this.$createElement;
        this.$notify({
          title: "复制成功：",
          message: h("i", { style: "color: teal" }, "快去分享给好朋友吧!"),
        });
        // console.log("复制成功");
        // 释放内存
        clipboard.destroy();
      });

      clipboard.on("error", () => {
        // 不支持复制
        Message({
          message: "该浏览器不支持自动复制",
          type: "warning",
        });
        // 释放内存
        clipboard.destroy();
      });
    },
    //获取所有内容
    getContent(uid) {
      touristApi
        .getContentByUid(uid,this.page)
        .then((response) => {
          this.contentObj = response.data.records;
          this.total = response.data.total;
        })
        .catch(() => {
        });
    },
    //获取用户信息
    getInfo(uid){
      touristApi
        .getInfo(uid)
        .then((response) => {
          this.infoObj = response.data;
        })
    },
    //当页数改变的时候
    handleCurrentChange(val) {
      this.page = val;
      this.getContent(this.uid,this.page);
    },
    //跳转评论
    pin(value) {
      this.$router.push({ path: '/function/comments', query: { cid: value } });
    },
    //点赞
    dz(cid) {
      if (cookie.get("campus-token") === undefined) {
        this.$router.push({ path: "/userlogin", query: { id: "1" } });
      } else {
        operateApi.getUserIsZan(cid).then((response) => {
          const text = response.data;
          this.$message({
            showClose: true,
            message: text,
            type: "success",
          });
          this.getContent(this.uid);
        });
      }
    },
    r(){
      this.$router.go(-1);
    },
    openLargeAvatar(imageUrl) {
      this.largeAvatarUrl = imageUrl; // 设置大头像的URL
      this.isDialogVisible = true;    // 显示对话框
    },
  }
}
</script>
<style>
.bg {
  background-color: rgba(255, 255, 255, 0.1);
  box-shadow: 0 25px 45px rgb(0 0 0 / 10%);
  border: 1px solid rgba(255, 255, 255, 0);
  border-radius: 10px;
}

.xxbox {
  margin: 15px 15px 30px 15px;
  /* box-shadow: 0 10px 12px 0 rgba(0, 0, 0, 0.2); */
  background-color: rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 25px rgb(0 0 0 / 10%);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
}

.ty {
  border-radius: 10px;
  padding: 6px;
}

.sub-title {
  padding-top: 20px;
  font-size: 15px;
  font-weight: bold;
}

.time {
  padding-top: 20px;
  float: right;
  font-size: 12px;
  color: darkgrey;
}

.sz {
  font-weight: bolder;
  font-size: 10px;
  color: rgb(110, 110, 110);
}

.fenye {
  width: 100%;
  margin: 0 auto;
  text-align: center;
}

.dz {
  text-align: center;
  margin: 5px;
}

.ssss {
  margin: 5px 0 0 5px;
}

.wwe {
  height: 10px;
}
.user-profile-header {
  display: flex;
  align-items: center;
  padding: 15px; /* 增加内边距 */
  background-color: #efefef;
  border-radius: 8px; /* 增大圆角半径 */
}

.avatar {
  margin-right: 15px; /* 增加与文本之间的距离 */
  cursor: pointer;
}

.user-info h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 8px;
}

.user-info .details {
  display: flex;
  flex-direction: column; /* 改变细节信息排列方式为垂直 */
}

.user-info .details span {
  margin: 2px 0; /* 细节信息间的间距 */
  font-size: 14px;
  color: #666; /* 使用较淡的颜色突出主要内容 */
}
.pre-wrap-text {
  white-space: pre-wrap; /* 保持空白符序列，但是正常地进行换行 */
}
.r{
  cursor: pointer;
}
</style>
